from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
import pandas as pd
import logging

# 设置日志记录
logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')

driver = webdriver.Chrome(service=Service('D:\\myproject\\feng-huang\\1.自动化\\1.selenium\\chromedriver.exe'))
driver.maximize_window()

data = []


def get_element_text(driver, xpath):
    try:
        element = driver.find_element(By.XPATH, xpath)
        return element.text
    except Exception as e:
        logging.error(f"未找到元素: {xpath}，错误信息: {e}")
        return ''


def get_element_sibling_text(driver, element, tag):
    try:
        sibling = element.find_element(By.XPATH, f"./following-sibling::{tag}[1]")
        return sibling.text
    except Exception as e:
        logging.error(f"未找到元素的兄弟节点: {tag}，错误信息: {e}")
        return ''


def get_elements_text(driver, class_name):
    try:
        elements = driver.find_elements(By.CLASS_NAME, class_name)
        return [element.text for element in elements]
    except Exception as e:
        logging.error(f"未找到元素列表: {class_name}，错误信息: {e}")
        return []


for i in range(1, 485):
    driver.get(f'https://www.foodtalks.cn/company/select?page={i}')
    time.sleep(5)

    names = driver.find_elements(By.XPATH,
                                 '/html/body/div[1]/div[2]/section/main/section/main/div[1]/div[2]/div[2]/div')

    for name in names:
        dict = {
            '简称': get_element_text(name, './a/div[2]/h2'),
            '产品类别': get_element_text(name, './a/div[2]/div[1]'),
            '排名': get_element_text(name, './a/div[2]/div[3]'),  # 假设排名在位置之后
            '位置': get_element_text(name, './a/div[2]/div[2]'),  # 假设位置在产品类别之后
            '产品': get_element_text(name, './a/div[2]/div[4]'),  # 假设产品在位置之后
            'url': name.find_element(By.XPATH, './a').get_attribute('href') if name.find_element(By.XPATH,
                                                                                                 './a') else ''
        }

        title = name.find_element(By.XPATH, './a/div[2]/h2')
        title.click()
        time.sleep(2)
        driver.switch_to.window(driver.window_handles[1])

        try:
            driver.find_element(By.XPATH, '//span[text()="首页"]').click()
        except Exception as e:
            logging.error(f"点击首页按钮时出错，错误信息: {e}")
            pass

        for key, value in {
            '公司名称': '//div[text()="中文全称："]',
            '公司简称': '//div[text()="中文简称："]',
            '英文名称': '//div[text()="英文全称："]',
            '英文简称': '//div[text()="英文简称："]',
            '公司类型': '//div[text()="公司类型："]',
            '成立时间': '//div[text()="成立时间："]',
            '股票代码': '//div[text()="股票代码："]',
            '公司总机': '//div[text()="公司总机："]',
            '公司性质': '//div[text()="公司性质："]',
            '行业分类': '//div[text()="行业分类："]',
            '主要品牌': '//div[text()="主要品牌："]',
            '主要产品': '//div[text()="主要产品："]',
            '公司官网': '//div[text()="公司官网："]',
            '公司总部': '//div[text()="公司总部："]',
            '总部地址': '//div[text()="总部地址："]',
        }.items():
            element = driver.find_element(By.XPATH, value)
            if element.text == f'{key}：':
                dict[key] = get_element_sibling_text(driver, element, 'div')

        try:
            unfold_more = driver.find_element(By.XPATH, '//span[text()="展开"]')
            unfold_more.click()
        except Exception as e:
            logging.error(f"点击展开按钮时出错，错误信息: {e}")
            pass

        dict['公司介绍'] = get_element_text(driver, '//span[text()="公司介绍"]/following-sibling::div[1]')

        for section, xpath in {
            '品牌': '//span[text()="品牌"]',
            '产品': '//span[text()="产品"]',
            '客户': '//span[text()="客户"]',
            '供应商': '//span[text()="供应商"]',
            '联系方式': '//span[text()="联系方式"]',
        }.items():
            try:
                button = driver.find_element(By.XPATH, xpath)
                button.click()
                time.sleep(1)
                dict[section] = get_elements_text(driver, 'el-row') if section != '联系方式' else get_element_text(
                    driver, '//div[@id="section-content"]')
            except Exception as e:
                logging.error(f"点击{section}按钮时出错，错误信息: {e}")
                pass

        driver.close()
        driver.switch_to.window(driver.window_handles[0])
        time.sleep(2)

        data.append(dict)

    print(f'第{i}页数据获取完成')
    # 仅在所有页面抓取完成后保存Excel
    if i % 20 == 0:  # 每20页保存一次，减少写入次数
        df = pd.DataFrame(data)
        df.to_excel(f'./data/第{i}页数据.xlsx', index=False, engine='openpyxl')

driver.quit()
